﻿package mortal.game.view.mainUI.avatar
{
    import Message.DB.Tables.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.manager.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.menu.*;

    public class BaseAvater extends GSprite
    {
        protected var _entityInfo:EntityInfo;
        protected var _avatarBg:GBitmap;
        protected var _level:GTextFiled;
        protected var _name:GTextFiled;
        protected var _shengmingBar:BaseProgressBar;
        protected var _avatar:GImageBitmap;
        protected var _bgSprite:ToolTipSprite;

        public function BaseAvater()
        {
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }// end function

        protected function onRemoveFromStage(event:Event) : void
        {
            this.removeEntityEvent();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bgSprite = UICompomentPool.getUICompoment(ToolTipSprite);
            this._bgSprite.createDisposedChildren();
            this._bgSprite.width = 100;
            this._bgSprite.height = 50;
            this.addChild(this._bgSprite);
            this._bgSprite.buttonMode = true;
            this._bgSprite.mouseEnabled = true;
            this._bgSprite.mouseChildren = false;
            this.addBg();
            this.addAvatar();
            this.addLifeBar();
            this.addName();
            this.addLevel();
            return;
        }// end function

        protected function addBg() : void
        {
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.AvatarMonsterBg, 5, 0, this));
            return;
        }// end function

        protected function addAvatar() : void
        {
            this._avatar = UIFactory.gImageBitmap("", 20, 22, this);
            return;
        }// end function

        protected function addLifeBar() : void
        {
            this._shengmingBar = UICompomentPool.getUICompoment(BaseProgressBar);
            this._shengmingBar.setProgress(ImagesConst.AvatarNpcLife, false, 2, 0, 152, 12);
            this._shengmingBar.isCacheBmp = true;
            if (Global.isDebugModle)
            {
                this._shengmingBar.setLabel(BaseProgressBar.ProgressBarTextOver, -35, -5);
            }
            else
            {
                this._shengmingBar.setLabel(BaseProgressBar.ProgressBarTextPercent, -15, -5);
            }
            this._shengmingBar.mouseChildren = false;
            this._shengmingBar.mouseEnabled = true;
            this._shengmingBar.x = 63;
            this._shengmingBar.y = 25;
            this.addChild(this._shengmingBar);
            return;
        }// end function

        protected function addName() : void
        {
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.size = 12;
            this._name = UIFactory.gTextField("", 80, 40, 125, 20, this, _loc_1, false);
            this._name.autoSize = TextFieldAutoSize.LEFT;
            return;
        }// end function

        protected function addLevel() : void
        {
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            PlayerMenuRegister.UnRegister(this._bgSprite);
            this._bgSprite.dispose(param1);
            this._bgSprite = null;
            this._level.dispose(param1);
            this._level = null;
            this._name.dispose(param1);
            this._name = null;
            this._avatar.dispose(param1);
            this._avatar = null;
            this._shengmingBar.dispose(param1);
            this._shengmingBar = null;
            super.disposeImpl(param1);
            return;
        }// end function

        public function updateLevel(event:Event = null) : void
        {
            return;
        }// end function

        public function updateName(event:Event = null) : void
        {
            if (!this._entityInfo)
            {
                return;
            }
            this._name.text = this._entityInfo.entityInfo.name;
            return;
        }// end function

        public function updateLife(event:Event = null) : void
        {
            if (!this._entityInfo)
            {
                return;
            }
            this._shengmingBar.setValue(this._entityInfo.entityInfo.life, this._entityInfo.entityInfo.maxLife);
            return;
        }// end function

        public function updateAvatar() : void
        {
            var _loc_1:TNpc = null;
            if (this._entityInfo)
            {
                this._avatar.bitmapData = null;
                _loc_1 = NPCConfig.instance.getInfoByCode(this._entityInfo.entityInfo.code);
                if (_loc_1)
                {
                    this._avatar.imgUrl = _loc_1.avatar + ".png";
                }
            }
            return;
        }// end function

        public function updateEntity(param1:EntityInfo) : void
        {
            this.removeEntityEvent();
            this._entityInfo = param1;
            if (this._entityInfo)
            {
                param1.addEventListener(EntityInfoEventName.DisposeEntity, this.removeEntityEvent);
            }
            this.updateAvatar();
            this.updateLevel();
            this.updateName();
            this.updateLife();
            this.addEntityEvent();
            return;
        }// end function

        public function stopUpdateEntity() : void
        {
            this.removeEntityEvent();
            return;
        }// end function

        protected function addEntityEvent() : void
        {
            if (this._entityInfo)
            {
                this._entityInfo.addEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
                this._entityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
                this._entityInfo.addEventListener(EntityInfoEventName.UpdateName, this.updateName);
            }
            return;
        }// end function

        protected function removeEntityEvent(event:EntityInfoEvent = null) : void
        {
            if (this._entityInfo)
            {
                this._entityInfo.removeEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
                this._entityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
                this._entityInfo.removeEventListener(EntityInfoEventName.DisposeEntity, this.removeEntityEvent);
                this._entityInfo.removeEventListener(EntityInfoEventName.UpdateName, this.updateName);
            }
            return;
        }// end function

        public function get entityInfo() : EntityInfo
        {
            return this._entityInfo;
        }// end function

    }
}
